package com.cencat.merchant.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cencat.merchant.entity.DriverMerchantBindEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 司机商家绑定关系数据访问层
 * 
 * @author cencat
 * @since 2024
 */
@Mapper
public interface DriverMerchantBindMapper extends BaseMapper<DriverMerchantBindEntity> {
    
    /**
     * 根据商家ID查询绑定关系
     * 
     * @param merchantId 商家ID
     * @return 绑定关系列表
     */
    @Select("SELECT * FROM biz_driver_merchant_bind WHERE merchant_id = #{merchantId} " +
            "AND bind_status = 1 AND deleted = 0 ORDER BY create_time DESC")
    List<DriverMerchantBindEntity> selectByMerchantId(@Param("merchantId") Long merchantId);
    
    /**
     * 根据司机ID查询绑定关系
     * 
     * @param driverId 司机ID
     * @return 绑定关系列表
     */
    @Select("SELECT * FROM biz_driver_merchant_bind WHERE driver_id = #{driverId} " +
            "AND bind_status = 1 AND deleted = 0 ORDER BY create_time DESC")
    List<DriverMerchantBindEntity> selectByDriverId(@Param("driverId") Long driverId);
    
    /**
     * 根据司机ID和商家ID查询绑定关系
     * 
     * @param driverId 司机ID
     * @param merchantId 商家ID
     * @return 绑定关系
     */
    @Select("SELECT * FROM biz_driver_merchant_bind WHERE driver_id = #{driverId} " +
            "AND merchant_id = #{merchantId} AND deleted = 0")
    DriverMerchantBindEntity selectByDriverIdAndMerchantId(@Param("driverId") Long driverId, 
                                                          @Param("merchantId") Long merchantId);
    
    /**
     * 根据绑定类型查询绑定关系
     * 
     * @param bindType 绑定类型
     * @return 绑定关系列表
     */
    @Select("SELECT * FROM biz_driver_merchant_bind WHERE bind_type = #{bindType} " +
            "AND bind_status = 1 AND deleted = 0 ORDER BY create_time DESC")
    List<DriverMerchantBindEntity> selectByBindType(@Param("bindType") Integer bindType);
    
    /**
     * 根据租户ID查询绑定关系
     * 
     * @param tenantId 租户ID
     * @return 绑定关系列表
     */
    @Select("SELECT * FROM biz_driver_merchant_bind WHERE tenant_id = #{tenantId} " +
            "AND deleted = 0 ORDER BY create_time DESC")
    List<DriverMerchantBindEntity> selectByTenantId(@Param("tenantId") Long tenantId);
    
    /**
     * 查询商家的专属司机
     * 
     * @param merchantId 商家ID
     * @return 专属司机绑定关系列表
     */
    @Select("SELECT * FROM biz_driver_merchant_bind WHERE merchant_id = #{merchantId} " +
            "AND bind_type = 1 AND bind_status = 1 AND deleted = 0 ORDER BY create_time DESC")
    List<DriverMerchantBindEntity> selectExclusiveDriversByMerchantId(@Param("merchantId") Long merchantId);
    
    /**
     * 查询司机的专属商家
     * 
     * @param driverId 司机ID
     * @return 专属商家绑定关系列表
     */
    @Select("SELECT * FROM biz_driver_merchant_bind WHERE driver_id = #{driverId} " +
            "AND bind_type = 1 AND bind_status = 1 AND deleted = 0 ORDER BY create_time DESC")
    List<DriverMerchantBindEntity> selectExclusiveMerchantsByDriverId(@Param("driverId") Long driverId);
    
    /**
     * 更新绑定状态
     * 
     * @param bindId 绑定ID
     * @param bindStatus 绑定状态
     * @return 更新行数
     */
    @Update("UPDATE biz_driver_merchant_bind SET bind_status = #{bindStatus}, update_time = NOW() WHERE id = #{bindId}")
    int updateBindStatus(@Param("bindId") Long bindId, @Param("bindStatus") Integer bindStatus);
    
    /**
     * 更新绑定类型
     * 
     * @param bindId 绑定ID
     * @param bindType 绑定类型
     * @return 更新行数
     */
    @Update("UPDATE biz_driver_merchant_bind SET bind_type = #{bindType}, update_time = NOW() WHERE id = #{bindId}")
    int updateBindType(@Param("bindId") Long bindId, @Param("bindType") Integer bindType);
    
    /**
     * 设置绑定生效时间
     * 
     * @param bindId 绑定ID
     * @param effectiveTime 生效时间
     * @return 更新行数
     */
    @Update("UPDATE biz_driver_merchant_bind SET effective_time = #{effectiveTime}, update_time = NOW() WHERE id = #{bindId}")
    int updateEffectiveTime(@Param("bindId") Long bindId, @Param("effectiveTime") LocalDateTime effectiveTime);
    
    /**
     * 设置绑定失效时间
     * 
     * @param bindId 绑定ID
     * @param expireTime 失效时间
     * @return 更新行数
     */
    @Update("UPDATE biz_driver_merchant_bind SET expire_time = #{expireTime}, update_time = NOW() WHERE id = #{bindId}")
    int updateExpireTime(@Param("bindId") Long bindId, @Param("expireTime") LocalDateTime expireTime);
    
    /**
     * 统计商家绑定的司机数量
     * 
     * @param merchantId 商家ID
     * @param bindType 绑定类型(可选)
     * @return 绑定司机数量
     */
    Long countDriversByMerchantId(@Param("merchantId") Long merchantId, @Param("bindType") Integer bindType);
    
    /**
     * 统计司机绑定的商家数量
     * 
     * @param driverId 司机ID
     * @param bindType 绑定类型(可选)
     * @return 绑定商家数量
     */
    Long countMerchantsByDriverId(@Param("driverId") Long driverId, @Param("bindType") Integer bindType);
    
    /**
     * 查询即将过期的绑定关系
     * 
     * @param hours 提前小时数
     * @return 即将过期的绑定关系列表
     */
    @Select("SELECT * FROM biz_driver_merchant_bind WHERE bind_status = 1 " +
            "AND expire_time IS NOT NULL " +
            "AND expire_time <= DATE_ADD(NOW(), INTERVAL #{hours} HOUR) " +
            "AND expire_time > NOW() " +
            "AND deleted = 0 ORDER BY expire_time")
    List<DriverMerchantBindEntity> selectExpiringBinds(@Param("hours") Integer hours);
    
    /**
     * 查询已过期的绑定关系
     * 
     * @return 已过期的绑定关系列表
     */
    @Select("SELECT * FROM biz_driver_merchant_bind WHERE bind_status = 1 " +
            "AND expire_time IS NOT NULL " +
            "AND expire_time <= NOW() " +
            "AND deleted = 0")
    List<DriverMerchantBindEntity> selectExpiredBinds();
    
    /**
     * 批量更新过期绑定状态
     * 
     * @return 更新行数
     */
    @Update("UPDATE biz_driver_merchant_bind SET bind_status = 0, update_time = NOW() " +
            "WHERE bind_status = 1 AND expire_time IS NOT NULL " +
            "AND expire_time <= NOW() AND deleted = 0")
    int batchUpdateExpiredBinds();
    
    /**
     * 根据时间范围查询绑定关系
     * 
     * @param tenantId 租户ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 绑定关系列表
     */
    @Select("SELECT * FROM biz_driver_merchant_bind WHERE tenant_id = #{tenantId} " +
            "AND create_time >= #{startTime} AND create_time <= #{endTime} " +
            "AND deleted = 0 ORDER BY create_time DESC")
    List<DriverMerchantBindEntity> selectByTimeRange(@Param("tenantId") Long tenantId,
                                                    @Param("startTime") LocalDateTime startTime,
                                                    @Param("endTime") LocalDateTime endTime);
}